home *** CD-ROM | disk | FTP | other *** search
/ Game Power / Game Power Vol. 1 (HEPP Computer) (1995).iso / S053 / DISK2 / EDIT.C < prev    next >
C/C++ Source or Header  |  1994-04-26  |  47KB  |  1,632 lines

  1. /*
  2.    Doom Editor Utility, by Brendon Wyber and Raphaël Quinet.
  3.  
  4.    You are allowed to use any parts of this code in another program, as
  5.    long as you give credits to the authors in the documentation and in
  6.    the program itself.  Read the file README.1ST for more information.
  7.  
  8.    This program comes with absolutely no warranty.
  9.  
  10.    EDIT.C - Editor routines.
  11. */
  12.  
  13. /* the includes */
  14. #include "deu.h"
  15. #include "levels.h"
  16. extern Bool InfoShown;        /* should we display the info bar? */
  17. int  MoveSpeed = 20;        /* movement speed */
  18.  
  19.  
  20. /*
  21.    the driving program
  22. */
  23.  
  24. void EditLevel( int episode, int mission, Bool newlevel)
  25. {
  26.    ReadWTextureNames();
  27.    ReadFTextureNames();
  28.    InitGfx();
  29.    CheckMouseDriver();
  30.    if (episode < 1 || mission < 1)
  31.       SelectLevel( &episode, &mission);
  32.    if (episode > 0 && mission > 0)
  33.    {
  34.       ClearScreen();
  35.       ReadLevelData( episode, mission);
  36.       if (newlevel)
  37.       {
  38.      ForgetLevelData();
  39.      MapMinX = -4000;
  40.      MapMinY = -4000;
  41.      MapMaxX = 4000;
  42.      MapMaxY = 4000;
  43.       }
  44.       LogMessage( ": Editing E%dM%d...\n", episode, mission);
  45.       EditorLoop( episode, mission);
  46.       LogMessage( ": Finished editing E%dM%d...\n", episode, mission);
  47.       TermGfx();
  48.       if (! Registered)
  49.      printf( "Please register DOOM if you want to be able to save your changes.\n");
  50.       ForgetLevelData();
  51.       /* forget the level pointer */
  52.       Level = NULL;
  53.    }
  54.    else
  55.       TermGfx();
  56.    ForgetWTextureNames();
  57.    ForgetFTextureNames();
  58. }
  59.  
  60.  
  61.  
  62. /*
  63.    select a level
  64. */
  65.  
  66. void SelectLevel( int *episode, int *mission)
  67. {
  68.    MDirPtr dir;
  69.    char name[ 7];
  70.    char **levels;
  71.    int n = 0;
  72.  
  73.    dir = MasterDir;
  74.    while (dir)
  75.    {
  76.       if (dir->dir.size == 0 && dir->dir.name[ 0] == 'E' && dir->dir.name[ 2] == 'M' && dir->dir.name[ 4] == '\0')
  77.       {
  78.      if (n == 0)
  79.         levels = GetMemory( sizeof( char *));
  80.      else
  81.         levels = ResizeMemory( levels, (n + 1) * sizeof( char *));
  82.      levels[ n] = dir->dir.name;
  83.      n++;
  84.       }
  85.       dir = dir->next;
  86.    }
  87.    if (*episode < 1)
  88.       *episode = 1;
  89.    if (*mission < 1)
  90.       *mission = 1;
  91.    sprintf( name, "E%dM%d", *episode, *mission);
  92.    InputNameFromList( -1, -1, "Select an episode and a mission number:", n, levels, name);
  93.    FreeMemory( levels);
  94.    if (*name)
  95.       sscanf( name, "E%dM%d", episode, mission);
  96.    else
  97.    {
  98.       *episode = 0;
  99.       *mission = 0;
  100.    }
  101. }
  102.  
  103.  
  104.  
  105. /*
  106.    get the name of the new WAD file
  107. */
  108.  
  109. char *GetWadFileName( int episode, int mission)
  110. {
  111.    char *outfile = GetMemory( 80);
  112.    char *dotp;
  113.    WadPtr wad;
  114.  
  115.    /* get the file name */
  116.    if (! strcmp(Level->wadfile->filename, MainWad))
  117.       sprintf( outfile, "E%dM%d.WAD", episode, mission);
  118.    else
  119.       strcpy( outfile, Level->wadfile->filename);
  120.    do
  121.    {
  122.       InputFileName( -1, -1, "Name of the new WAD file:", 79, outfile);
  123.    }
  124.    while (!strcmp(outfile, MainWad));
  125.    /* escape */
  126.    if (outfile[ 0] == '\0')
  127.    {
  128.       FreeMemory( outfile);
  129.       return NULL;
  130.    }
  131.    /* if the WAD file already exists, rename it to "*.BAK" */
  132.    for (wad = WadFileList; wad; wad = wad->next)
  133.       if (!stricmp( outfile, wad->filename))
  134.       {
  135.      dotp = strrchr( wad->filename, '.');
  136.      if (dotp == NULL)
  137.         strcat( wad->filename, ".BAK");
  138.      else
  139.         strcpy( dotp, ".BAK");
  140.      /* need to close, then reopen: problems with SHARE.EXE */
  141.      fclose( wad->fileinfo);
  142.      if (rename( outfile, wad->filename) < 0)
  143.      {
  144.         if (unlink( wad->filename) < 0)
  145.            ProgError( "could not delete file \"%s\"", wad->filename);
  146.         if (rename( outfile, wad->filename) < 0)
  147.            ProgError( "could not rename \"%s\" to \"%s\"", outfile, wad->filename);
  148.      }
  149.      wad->fileinfo = fopen( wad->filename, "rb");
  150.      if (wad->fileinfo == NULL)
  151.         ProgError( "could not reopen file \"%s\"", wad->filename);
  152.      break;
  153.       }
  154.    return outfile;
  155. }
  156.  
  157.  
  158.  
  159. /*
  160.    display the help screen
  161.  */
  162.  
  163. void DisplayHelp( int objtype, int grid) /* SWAP! */
  164. {
  165.    int x0 = 137;
  166.    int y0 = 50;
  167.  
  168.    if (UseMouse)
  169.       HideMousePointer();
  170.    /* put in the instructions */
  171.    DrawScreenBox3D( x0, y0, x0 + 364, y0 + 355);
  172.    SetColor( LIGHTCYAN);
  173.    DrawScreenText( x0 + 100, y0 + 20, "Doom Editor Utility");
  174.    DrawScreenText( 269 - strlen(GetEditModeName( objtype)) * 4, y0 + 32, "- %s Editor -", GetEditModeName( objtype));
  175.    SetColor( BLACK);
  176.    DrawScreenText( x0 + 10, y0 + 60, "Use the mouse or the cursor keys to move");
  177.    DrawScreenText( -1, -1, "around.  The map scrolls when the pointer");
  178.    DrawScreenText( -1, -1, "reaches the edge of the screen.");
  179.    DrawScreenText( -1, y0 + 100, "Other useful keys are:");
  180.    if (Registered)
  181.       DrawScreenText( -1, y0 + 115, "Q     - Quit, saving changes");
  182.    else
  183.    {
  184.       SetColor( DARKGRAY);
  185.       DrawScreenText( -1, y0 + 115, "Q     - Quit without saving changes");
  186.       SetColor( BLACK);
  187.    }
  188.    DrawScreenText( -1, -1, "Esc   - Exit without saving changes");
  189.    DrawScreenText( -1, -1, "Tab   - Switch to the next editing mode");
  190.    DrawScreenText( -1, -1, "Space - Change the move/scroll speed");
  191.    DrawScreenText( -1, -1, "+/-   - Change the map scale (current: %d)", Scale);
  192.    DrawScreenText( -1, -1, "G     - Change the grid scale (cur.: %d)", grid);
  193.    DrawScreenText( -1, -1, "N, >  - Jump to the next object.");
  194.    DrawScreenText( -1, -1, "P, <  - Jump to the previous object.");
  195.    DrawScreenText( -1, -1, "J, #  - Jump to a specific object (enter #)");
  196.    DrawScreenText( -1, -1, "M     - Mark/unmark current object (select)");
  197.    if (objtype == OBJ_THINGS || objtype == OBJ_VERTEXES)
  198.       DrawScreenText( -1, -1, "D     - Toggle drag mode");
  199.    else
  200.       DrawScreenText( -1, -1, "C     - Clear all marks and redraw map");
  201.    DrawScreenText( -1, -1, "Ins   - Insert a new object");
  202.    DrawScreenText( -1, -1, "Del   - Delete the current object");
  203.    DrawScreenText( -1, -1, "Enter - Edit the current/selected object(s)");
  204.    DrawScreenText( -1, y0 + 265, "Mouse buttons:");
  205.    if (SwapButtons)
  206.    {
  207.       DrawScreenText( -1, y0 + 280, "Left  - Edit the current/selected object(s)");
  208.       DrawScreenText( -1, -1, "Middle- Mark/unmark the current object.");
  209.    }
  210.    else
  211.    {
  212.       DrawScreenText( -1, y0 + 280, "Left  - Mark/unmark the current object");
  213.       DrawScreenText( -1, -1, "Middle- Edit the current/selected object(s)");
  214.    }
  215.    DrawScreenText( -1, -1, "Right - Drag the current/selected object(s)");
  216.    DrawScreenText( -1, y0 + 320, "Please read DEU.TXT for more information");
  217.    SetColor( YELLOW);
  218.    DrawScreenText( -1, y0 + 340, "Press any key to return to the editor...");
  219.    bioskey( 0);
  220.    if (UseMouse)
  221.       ShowMousePointer();
  222. }
  223.  
  224.  
  225.  
  226. /*
  227.    the editor main loop
  228. */
  229.  
  230. void EditorLoop( int episode, int mission) /* SWAP! */
  231. {
  232.    int    EditMode = OBJ_THINGS;
  233.    int    CurObject = -1;
  234.    int    OldObject = -1;
  235.    Bool   RedrawMap = TRUE;
  236.    Bool   RedrawObj = FALSE;
  237.    Bool   DragObject = FALSE;
  238.    int    key, altkey, buttons, oldbuttons;
  239.    int    GridScale = 0;
  240.    SelPtr Selected = NULL;
  241.    char   keychar;
  242.    int    SelBoxX = 0;
  243.    int    SelBoxY = 0;
  244.    Bool   StretchSelBox = FALSE;
  245.    Bool   ShowRulers = FALSE;
  246.  
  247.    MadeChanges = FALSE;
  248.    MadeMapChanges = FALSE;
  249.    if (InitialScale < 1)
  250.       InitialScale = 1;
  251.    else if (InitialScale > 20)
  252.       InitialScale = 20;
  253.    Scale = InitialScale;
  254.    CenterMapAroundCoords( (MapMinX + MapMaxX) / 2, (MapMinY + MapMaxY) / 2);
  255.    if (UseMouse)
  256.    {
  257.       ResetMouseLimits();
  258.       SetMouseCoords( PointerX, PointerY);
  259.       ShowMousePointer();
  260.       oldbuttons = 0;
  261.    }
  262.    else
  263.       FakeCursor = TRUE;
  264.  
  265.    for (;;)
  266.    {
  267.       key = 0;
  268.       altkey = 0;
  269.  
  270.       /* get mouse position and button status */
  271.       if (UseMouse)
  272.       {
  273.      if (FakeCursor || ShowRulers)
  274.      {
  275.         HideMousePointer();
  276.         DrawPointer( ShowRulers);
  277.         ShowMousePointer();
  278.      }
  279.      GetMouseCoords( &PointerX, &PointerY, &buttons);
  280.      if (FakeCursor || ShowRulers)
  281.      {
  282.         HideMousePointer();
  283.         DrawPointer( ShowRulers);
  284.         ShowMousePointer();
  285.      }
  286.      if ( buttons == 1 && PointerY < 17)
  287.      {
  288.         /* kluge for the menu bar */
  289.         altkey = 0x08;
  290.         if (PointerX < 12)
  291.            Beep();
  292.         else if (PointerX < 60)
  293.            key = 0x2100; /* 'F' */
  294.         else if (PointerX < 108)
  295.            key = 0x1200; /* 'E' */
  296.         else if (PointerX < 172)
  297.            key = 0x1f00; /* 'S' */
  298.         else if (PointerX < 228)
  299.            key = 0x3200; /* 'M' */
  300.         else if (PointerX < 276)
  301.            key = 0x1700; /* 'I' */
  302.         else if (PointerX < 348)
  303.            key = 0x1800; /* 'O' */
  304.         else if (PointerX < 406)
  305.            key = 0x2E00; /* 'C' */
  306.         else if (PointerX < ScrMaxX - 43)
  307.            Beep();
  308.         else
  309.            key = 0x2300; /* 'H' */
  310.      }
  311.      else
  312.      {
  313.         if (buttons != oldbuttons)
  314.         {
  315.            switch (buttons)
  316.            {
  317.            case 1:
  318.           if (SwapButtons)
  319.              key = 0x000D;
  320.           else
  321.              key = 'M';      /* Press left button = Mark/Unmark ('M') */
  322.           break;
  323.            case 2:
  324.           if (! DragObject)
  325.              key = 'D';      /* Press right button = Drag */
  326.           break;
  327.            case 3:
  328.            case 4:
  329.           if (SwapButtons)
  330.              key = 'M';
  331.           else
  332.              key = 0x000D;   /* Press middle button = Edit ('Enter') */
  333.           break;
  334.            default:
  335.           if (StretchSelBox) /* Release left button = End Selection Box */
  336.              key = 'M';
  337.           if (DragObject)    /* Release right button = End Drag */
  338.              key = 'D';
  339.           break;
  340.            }
  341.            altkey = bioskey( 2);
  342.         }
  343.      }
  344.      oldbuttons = buttons;
  345.       }
  346.  
  347.       /* drag object(s) */
  348.       if (DragObject)
  349.       {
  350.      int forgetit = FALSE;
  351.  
  352.      if (IsSelected( Selected, CurObject) == FALSE)
  353.         ForgetSelection( &Selected);
  354.      else if (Selected->objnum != CurObject)
  355.      {
  356.         /* current object must be first in the list */
  357.         UnSelectObject( &Selected, CurObject);
  358.         SelectObject( &Selected, CurObject);
  359.      }
  360.      if (Selected == NULL && CurObject >= 0)
  361.      {
  362.         SelectObject( &Selected, CurObject);
  363.         forgetit = TRUE;
  364.      }
  365.      if (Selected)
  366.      {
  367.         if (MoveObjectsToCoords( EditMode, Selected, MAPX( PointerX), MAPY( PointerY), GridScale))
  368.            RedrawMap = TRUE;
  369.         if (forgetit)
  370.            ForgetSelection( &Selected);
  371.      }
  372.      else
  373.      {
  374.         Beep();
  375.         DragObject = FALSE;
  376.      }
  377.       }
  378.       else if (StretchSelBox)
  379.       {
  380.      int x = MAPX( PointerX);
  381.      int y = MAPY( PointerY);
  382.  
  383.      /* draw selection box */
  384.      SetColor( CYAN);
  385.      setwritemode( XOR_PUT);
  386.      if (UseMouse)
  387.         HideMousePointer();
  388.      DrawMapLine( SelBoxX, SelBoxY, SelBoxX, y);
  389.      DrawMapLine( SelBoxX, y, x, y);
  390.      DrawMapLine( x, y, x, SelBoxY);
  391.      DrawMapLine( x, SelBoxY, SelBoxX, SelBoxY);
  392.      if (UseMouse)
  393.         ShowMousePointer();
  394.      delay( 50);
  395.      if (UseMouse)
  396.         HideMousePointer();
  397.      DrawMapLine( SelBoxX, SelBoxY, SelBoxX, y);
  398.      DrawMapLine( SelBoxX, y, x, y);
  399.      DrawMapLine( x, y, x, SelBoxY);
  400.      DrawMapLine( x, SelBoxY, SelBoxX, SelBoxY);
  401.      setwritemode( COPY_PUT);
  402.      if (UseMouse)
  403.         ShowMousePointer();
  404.       }
  405.       else if (!RedrawObj)
  406.       {
  407.      /* check if there is something near the pointer */
  408.      OldObject = CurObject;
  409.      if ((bioskey( 2) & 0x03) == 0x00)  /* no shift keys */
  410.         CurObject = GetCurObject( EditMode, MAPX( PointerX - 4), MAPY( PointerY - 4), MAPX( PointerX + 4), MAPY( PointerY + 4));
  411.      if (CurObject < 0)
  412.         CurObject = OldObject;
  413.       }
  414.  
  415.       /* draw the map */
  416.       if (RedrawMap)
  417.       {
  418.      if (UseMouse)
  419.         HideMousePointer();
  420.      DrawMap( EditMode, GridScale);
  421.      HighlightSelection( EditMode, Selected);
  422.      if (UseMouse)
  423.         ShowMousePointer();
  424.       }
  425.  
  426.       /* highlight the current object and display the information box */
  427.       if (RedrawMap || CurObject != OldObject || RedrawObj)
  428.       {
  429.      RedrawObj = FALSE;
  430.      if (UseMouse)
  431.         HideMousePointer();
  432.      if (!RedrawMap && OldObject >= 0)
  433.         HighlightObject( EditMode, OldObject, YELLOW);
  434.      if (CurObject != OldObject)
  435.      {
  436.         PlaySound( 50, 10);
  437.         OldObject = CurObject;
  438.      }
  439.      if (CurObject >= 0)
  440.         HighlightObject( EditMode, CurObject, YELLOW);
  441.      if (bioskey( 1)) /* speedup */
  442.         RedrawObj = TRUE;
  443.      else
  444.         DisplayObjectInfo( EditMode, CurObject);
  445.      if (UseMouse)
  446.         ShowMousePointer();
  447.       }
  448.       if (RedrawMap && (FakeCursor || ShowRulers))
  449.       {
  450.      if (UseMouse)
  451.         HideMousePointer();
  452.      DrawPointer( ShowRulers);
  453.      if (UseMouse)
  454.         ShowMousePointer();
  455.       }
  456.  
  457.       /* the map is up to date */
  458.       RedrawMap = FALSE;
  459.  
  460.       /* get user input */
  461.       if (bioskey( 1) || key)
  462.       {
  463.      if (! key)
  464.      {
  465.         key = bioskey( 0);
  466.         altkey = bioskey( 2);
  467.      }
  468.  
  469.      /* user wants to access the drop-down menus */
  470.      if (altkey & 0x08)    /* if alt is pressed... */
  471.      {
  472.         if ((key & 0xFF00) == 0x2100)       /* Scan code for F */
  473.            key = PullDownMenu( 18, 19,
  474.                    "Save         F2", 0x3C00,    (int) 'S', 1,
  475.                    "Save As ExMx F3", 0x3D00,    (int) 'A', 6,
  476.                    "Print          ", -1,        (int) 'P', -1,
  477.                    "Quit          Q", (int) 'Q', (int) 'Q', 1,
  478.                    NULL);
  479.         else if ((key & 0xFF00) == 0x1200)  /* Scan code for E */
  480.         {
  481.            key = PullDownMenu( 66, 19,
  482.                    "Copy object(s)      O", (int) 'O', (int) 'C', 1,
  483.                    "Add object        Ins", 0x5200,    (int) 'A', 1,
  484.                    "Delete object(s)  Del", 0x5300,    (int) 'D', 1,
  485.                    ((EditMode == OBJ_VERTEXES) ?
  486.                    NULL :
  487.                    "Preferences        F5"), 0x3F00,   (int) 'P', 1,
  488.                    NULL);
  489.         }
  490.         else if ((key & 0xFF00) == 0x1F00)  /* Scan code for S */
  491.            key = PullDownMenu( 114, 19,
  492.                    "Find/Change       F4", -1,        (int) 'F', -1,
  493.                    "Repeat last find    ", -1,        (int) 'R', -1,
  494.                    "Next object        N", (int) 'N', (int) 'N', 1,
  495.                    "Prev object        P", (int) 'P', (int) 'P', 1,
  496.                    "Jump to object...  J", (int) 'J', (int) 'J', 1,
  497.                    NULL);
  498.         else if ((key & 0xFF00) == 0x3200)  /* Scan code for M */
  499.            key = PullDownMenu( 178, 19,
  500.                    ((EditMode == OBJ_THINGS) ?
  501.                    "√ Things              T" :
  502.                    "  Things              T"), (int) 'T', (int) 'T', 3,
  503.                    ((EditMode == OBJ_LINEDEFS) ?
  504.                    "√ Linedefs+Sidedefs   L" :
  505.                    "  Linedefs+Sidedefs   L"), (int) 'L', (int) 'L', 3,
  506.                    ((EditMode == OBJ_VERTEXES) ?
  507.                    "√ Vertexes            V" :
  508.                    "  Vertexes            V"), (int) 'V', (int) 'V', 3,
  509.                    ((EditMode == OBJ_SECTORS) ?
  510.                    "√ Sectors             S" :
  511.                    "  Sectors             S"), (int) 'S', (int) 'S', 3,
  512.                    "  Next mode         Tab",  0x0009,    (int) 'N', 3,
  513.                    "  Last mode   Shift+Tab",  0x0F00,    (int) 'L', 3,
  514.                    "  3D Preview          3",  (int) '3', (int) '3', -1,
  515.                    NULL);
  516.         else if ((key & 0xFF00) == 0x1700)  /* Scan code for I */
  517.         {
  518.            key = 0;
  519.            /* code duplicated from 'F8' - I hate to do that */
  520.            if (Selected)
  521.           MiscOperations( 234, 19, EditMode, &Selected);
  522.            else
  523.            {
  524.           if (CurObject >= 0)
  525.              SelectObject( &Selected, CurObject);
  526.           MiscOperations( 234, 19, EditMode, &Selected);
  527.           if (CurObject >= 0)
  528.              UnSelectObject( &Selected, CurObject);
  529.            }
  530.            CurObject = -1;
  531.            DragObject = FALSE;
  532.            StretchSelBox = FALSE;
  533.         }
  534.         else if ((key & 0xFF00) == 0x1800)  /* Scan code for O */
  535.         {
  536.            int savednum, i;
  537.  
  538.            key = 0;
  539.            /* don't want to create the object behind the menu bar... */
  540.            if (PointerY < 20)
  541.            {
  542.           PointerX = ScrCenterX;
  543.           PointerY = ScrCenterY;
  544.            }
  545.            /* code duplicated from 'F9' - I hate to do that */
  546.            savednum = NumLineDefs;
  547.            InsertStandardObject( 282, 19, MAPX( PointerX), MAPY( PointerY));
  548.            if (NumLineDefs > savednum)
  549.            {
  550.           ForgetSelection( &Selected);
  551.           EditMode = OBJ_LINEDEFS;
  552.           for (i = savednum; i < NumLineDefs; i++)
  553.              SelectObject( &Selected, i);
  554.           CurObject = NumLineDefs - 1;
  555.           OldObject = -1;
  556.           DragObject = FALSE;
  557.           StretchSelBox = FALSE;
  558.            }
  559.         }
  560.         else if ((key & 0xFF00) == 0x2E00)  /* Scan code for C */
  561.         {
  562.            key = 0;
  563.            CheckLevel( 354, 19);
  564.         }
  565.         else if ((key & 0xFF00) == 0x2300)  /* Scan code for H */
  566.            key = PullDownMenu( ScrMaxX, 19,
  567.                    "  Keyboard & mouse  F1",  0x3B00,    (int) 'K', 3,
  568.                    (InfoShown ?
  569.                    "√ Info bar           I" :
  570.                    "  Info bar           I"), (int) 'I', (int) 'I', 3,
  571.                    "  About DEU...        ",  -1,        (int) 'A', -1,
  572.                    NULL);
  573.         else
  574.         {
  575.            Beep();
  576.            key = 0;
  577.         }
  578.         RedrawMap = TRUE;
  579.      }
  580.  
  581.      /* User wants to do the impossible. */
  582.      if (key == -1)
  583.      {
  584.         NotImplemented();
  585.         RedrawMap = TRUE;
  586.      }
  587.  
  588.      /* simplify the checks later on */
  589.      if (isprint(key & 0x00ff))
  590.         keychar = toupper(key);
  591.      else
  592.         keychar = '\0';
  593.  
  594.      /* erase the (keyboard) pointer */
  595.      if (FakeCursor || ShowRulers)
  596.      {
  597.         HideMousePointer();
  598.         DrawPointer( ShowRulers);
  599.         ShowMousePointer();
  600.      }
  601.  
  602.      /* user wants to exit */
  603.      if (keychar == 'Q')
  604.      {
  605.         ForgetSelection( &Selected);
  606.         if (Registered && MadeChanges)
  607.         {
  608.            char *outfile;
  609.  
  610.            outfile = GetWadFileName( episode, mission);
  611.            if (outfile)
  612.            {
  613.           SaveLevelData( outfile);
  614.           break;
  615.            }
  616.            else
  617.           RedrawMap = TRUE;
  618.         }
  619.         else
  620.            break;
  621.      }
  622.      else if ((key & 0x00FF) == 0x001B) /* 'Esc' */
  623.      {
  624.         if (DragObject)
  625.            DragObject = FALSE;
  626.         else if (StretchSelBox)
  627.            StretchSelBox = FALSE;
  628.         else
  629.         {
  630.            ForgetSelection( &Selected);
  631.            if (!MadeChanges || Confirm(-1, -1, "You have unsaved changes.  Do you really want to quit?", NULL))
  632.           break;
  633.            RedrawMap = TRUE;
  634.         }
  635.      }
  636.  
  637.      /* user is lost */
  638.      else if ((key & 0xFF00) == 0x3B00) /* 'F1' */
  639.      {
  640.         DisplayHelp( EditMode, GridScale);
  641.         RedrawMap = TRUE;
  642.      }
  643.  
  644.      /* user wants to save the level data */
  645.      else if ((key & 0xFF00) == 0x3C00 && Registered) /* 'F2' */
  646.      {
  647.         char *outfile;
  648.  
  649.         outfile = GetWadFileName( episode, mission);
  650.         if (outfile)
  651.            SaveLevelData( outfile);
  652.         RedrawMap = TRUE;
  653.      }
  654.  
  655.      /* user wants to save and change the episode and mission numbers */
  656.      else if ((key & 0xFF00) == 0x3D00 && Registered) /* 'F3' */
  657.      {
  658.         char *outfile;
  659.         int   e, m;
  660.         MDirPtr newLevel, oldl, newl;
  661.         char name[ 7];
  662.  
  663.         outfile = GetWadFileName( episode, mission);
  664.         if (outfile)
  665.         {
  666.            e = episode;
  667.            m = mission;
  668.            SelectLevel( &e, &m);
  669.            if (e > 0 && m > 0 && (e != episode || m != mission))
  670.            {
  671.           /* horrible but it works... */
  672.           episode = e;
  673.           mission = m;
  674.           sprintf( name, "E%dM%d", episode, mission);
  675.           newLevel = FindMasterDir( MasterDir, name);
  676.           oldl = Level;
  677.           newl = newLevel;
  678.           for (m = 0; m < 11; m++)
  679.           {
  680.              newl->wadfile = oldl->wadfile;
  681.              if (m > 0)
  682.             newl->dir = oldl->dir;
  683.              /*
  684.              if (!strcmp( outfile, oldl->wadfile->filename))
  685.              {
  686.             oldl->wadfile = WadFileList;
  687.             oldl->dir = lost...
  688.              }
  689.              */
  690.              oldl = oldl->next;
  691.              newl = newl->next;
  692.           }
  693.           Level = newLevel;
  694.            }
  695.            SaveLevelData( outfile);
  696.         }
  697.         RedrawMap = TRUE;
  698.      }
  699.  
  700.      /* user wants to get the 'Preferences' menu */
  701.      else if ((key & 0xFF00) == 0x3F00) /* 'F5' */
  702.      {
  703.         Preferences( -1, -1);
  704.         RedrawMap = TRUE;
  705.      }
  706.      /* user wants to get the menu of misc. ops */
  707.      else if ((key & 0xFF00) == 0x4200) /* 'F8' */
  708.      {
  709.         if (Selected)
  710.            MiscOperations( -1, -1, EditMode, &Selected);
  711.         else
  712.         {
  713.            if (CurObject >= 0)
  714.           SelectObject( &Selected, CurObject);
  715.            MiscOperations( -1, -1, EditMode, &Selected);
  716.            if (CurObject >= 0)
  717.           UnSelectObject( &Selected, CurObject);
  718.         }
  719.         CurObject = -1;
  720.         RedrawMap = TRUE;
  721.         DragObject = FALSE;
  722.         StretchSelBox = FALSE;
  723.      }
  724.  
  725.      /* user wants to insert a standard shape */
  726.      else if ((key & 0xFF00) == 0x4300) /* 'F9' */
  727.      {
  728.         int savednum, i;
  729.  
  730.         savednum = NumLineDefs;
  731.         InsertStandardObject( -1, -1, MAPX( PointerX), MAPY( PointerY));
  732.         if (NumLineDefs > savednum)
  733.         {
  734.            ForgetSelection( &Selected);
  735.            EditMode = OBJ_LINEDEFS;
  736.            for (i = savednum; i < NumLineDefs; i++)
  737.           SelectObject( &Selected, i);
  738.            CurObject = NumLineDefs - 1;
  739.            OldObject = -1;
  740.            DragObject = FALSE;
  741.            StretchSelBox = FALSE;
  742.         }
  743.         RedrawMap = TRUE;
  744.      }
  745.  
  746.      /* user wants to check his level */
  747.      else if ((key & 0xFF00) == 0x4400) /* 'F10' */
  748.      {
  749.         CheckLevel( -1, -1);
  750.         RedrawMap = TRUE;
  751.      }
  752.  
  753.      /* user wants to display/hide the info box */
  754.      else if (keychar == 'I')
  755.      {
  756.         InfoShown = !InfoShown;
  757.         RedrawMap = TRUE;
  758.      }
  759.  
  760.      /* user wants to change the scale */
  761.      else if ((keychar == '+' || keychar == '=') && Scale > 1)
  762.      {
  763.         OrigX += (PointerX - ScrCenterX) * Scale;
  764.         OrigY += (ScrCenterY - PointerY) * Scale;
  765.         Scale--;
  766.         OrigX -= (PointerX - ScrCenterX) * Scale;
  767.         OrigY -= (ScrCenterY - PointerY) * Scale;
  768.         RedrawMap = TRUE;
  769.      }
  770.      else if ((keychar == '-' || keychar == '_') && Scale < 20)
  771.      {
  772.         OrigX += (PointerX - ScrCenterX) * Scale;
  773.         OrigY += (ScrCenterY - PointerY) * Scale;
  774.         Scale++;
  775.         OrigX -= (PointerX - ScrCenterX) * Scale;
  776.         OrigY -= (ScrCenterY - PointerY) * Scale;
  777.         RedrawMap = TRUE;
  778.      }
  779.  
  780.      /* user wants to move */
  781.      else if ((key & 0xFF00) == 0x4800 && (PointerY - MoveSpeed) >= 0)
  782.      {
  783.         if (UseMouse)
  784.            SetMouseCoords( PointerX, PointerY - MoveSpeed);
  785.         else
  786.            PointerY -= MoveSpeed;
  787.      }
  788.      else if ((key & 0xFF00) == 0x5000 && (PointerY + MoveSpeed) <= ScrMaxY)
  789.      {
  790.         if (UseMouse)
  791.            SetMouseCoords( PointerX, PointerY + MoveSpeed);
  792.         else
  793.            PointerY += MoveSpeed;
  794.      }
  795.      else if ((key & 0xFF00) == 0x4B00 && (PointerX - MoveSpeed) >= 0)
  796.      {
  797.         if (UseMouse)
  798.            SetMouseCoords( PointerX - MoveSpeed, PointerY);
  799.         else
  800.            PointerX -= MoveSpeed;
  801.      }
  802.      else if ((key & 0xFF00) == 0x4D00 && (PointerX + MoveSpeed) <= ScrMaxX)
  803.      {
  804.         if (UseMouse)
  805.            SetMouseCoords( PointerX + MoveSpeed, PointerY);
  806.         else
  807.            PointerX += MoveSpeed;
  808.      }
  809.  
  810.      /* user wants so scroll the map */
  811.      else if ((key & 0xFF00) == 0x4900 && MAPY( ScrCenterY) < MapMaxY)
  812.      {
  813.         OrigY += MoveSpeed * 2 * Scale;
  814.         RedrawMap = TRUE;
  815.      }
  816.      else if ((key & 0xFF00) == 0x5100 && MAPY( ScrCenterY) > MapMinY)
  817.      {
  818.         OrigY -= MoveSpeed * 2 * Scale;
  819.         RedrawMap = TRUE;
  820.      }
  821.      else if ((key & 0xFF00) == 0x4700 && MAPX( ScrCenterX) > MapMinX)
  822.      {
  823.         OrigX -= MoveSpeed * 2 * Scale;
  824.         RedrawMap = TRUE;
  825.      }
  826.      else if ((key & 0xFF00) == 0x4F00 && MAPX( ScrCenterX) < MapMaxX)
  827.      {
  828.         OrigX += MoveSpeed * 2 * Scale;
  829.         RedrawMap = TRUE;
  830.      }
  831.  
  832.      /* user wants to change the movement speed */
  833.      else if (keychar == ' ')
  834.         MoveSpeed = MoveSpeed == 1 ? 20 : 1;
  835.  
  836.      /* user wants to change the edit mode */
  837.      else if ((key & 0x00FF) == 0x0009 || (key & 0xFF00) == 0x0F00 || keychar == 'T' || keychar == 'V' || keychar == 'L' || keychar == 'S')
  838.      {
  839.         int    PrevMode = EditMode;
  840.         SelPtr NewSel;
  841.  
  842.         if ((key & 0x00FF) == 0x0009) /* 'Tab' */
  843.         {
  844.            switch (EditMode)
  845.            {
  846.            case OBJ_THINGS:
  847.           EditMode = OBJ_VERTEXES;
  848.           break;
  849.            case OBJ_VERTEXES:
  850.           EditMode = OBJ_LINEDEFS;
  851.           break;
  852.            case OBJ_LINEDEFS:
  853.           EditMode = OBJ_SECTORS;
  854.           break;
  855.            case OBJ_SECTORS:
  856.           EditMode = OBJ_THINGS;
  857.           break;
  858.            }
  859.         }
  860.         else if ((key & 0xFF00) == 0x0F00) /* 'Shift-Tab' */
  861.         {
  862.            switch (EditMode)
  863.            {
  864.            case OBJ_THINGS:
  865.           EditMode = OBJ_SECTORS;
  866.           break;
  867.            case OBJ_VERTEXES:
  868.           EditMode = OBJ_THINGS;
  869.           break;
  870.            case OBJ_LINEDEFS:
  871.           EditMode = OBJ_VERTEXES;
  872.           break;
  873.            case OBJ_SECTORS:
  874.           EditMode = OBJ_LINEDEFS;
  875.           break;
  876.            }
  877.         }
  878.         else if (keychar == 'T')
  879.            EditMode = OBJ_THINGS;
  880.         else if (keychar == 'V')
  881.            EditMode = OBJ_VERTEXES;
  882.         else if (keychar == 'L')
  883.            EditMode = OBJ_LINEDEFS;
  884.         else if (keychar == 'S')
  885.            EditMode = OBJ_SECTORS;
  886.         /* special cases for the selection list... */
  887.         if (Selected)
  888.         {
  889.            /* select all LineDefs bound to the selected Sectors */
  890.            if (PrevMode == OBJ_SECTORS && EditMode == OBJ_LINEDEFS)
  891.            {
  892.           int l, sd;
  893.  
  894.           ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  895.           NewSel = NULL;
  896.           for (l = 0; l < NumLineDefs; l++)
  897.           {
  898.              sd = LineDefs[ l].sidedef1;
  899.              if (sd >= 0 && IsSelected( Selected, SideDefs[ sd].sector))
  900.             SelectObject( &NewSel, l);
  901.              else
  902.              {
  903.             sd = LineDefs[ l].sidedef2;
  904.             if (sd >= 0 && IsSelected( Selected, SideDefs[ sd].sector))
  905.                SelectObject( &NewSel, l);
  906.              }
  907.           }
  908.           ForgetSelection( &Selected);
  909.           Selected = NewSel;
  910.            }
  911.            /* select all Vertices bound to the selected LineDefs */
  912.            else if (PrevMode == OBJ_LINEDEFS && EditMode == OBJ_VERTEXES)
  913.            {
  914.           ObjectsNeeded( OBJ_LINEDEFS, 0);
  915.           NewSel = NULL;
  916.           while (Selected)
  917.           {
  918.              if (!IsSelected( NewSel, LineDefs[ Selected->objnum].start))
  919.             SelectObject( &NewSel, LineDefs[ Selected->objnum].start);
  920.              if (!IsSelected( NewSel, LineDefs[ Selected->objnum].end))
  921.             SelectObject( &NewSel, LineDefs[ Selected->objnum].end);
  922.              UnSelectObject( &Selected, Selected->objnum);
  923.           }
  924.           Selected = NewSel;
  925.            }
  926.            /* select all Sectors that have their LineDefs selected */
  927.            else if (PrevMode == OBJ_LINEDEFS && EditMode == OBJ_SECTORS)
  928.            {
  929.           int l, sd;
  930.  
  931.           ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  932.           NewSel = NULL;
  933.           /* select all Sectors... */
  934.           for (l = 0; l < NumSectors; l++)
  935.              SelectObject( &NewSel, l);
  936.           /* ... then unselect those that should not be in the list */
  937.           for (l = 0; l < NumLineDefs; l++)
  938.              if (!IsSelected( Selected, l))
  939.              {
  940.             sd = LineDefs[ l].sidedef1;
  941.             if (sd >= 0)
  942.                UnSelectObject( &NewSel, SideDefs[ sd].sector);
  943.             sd = LineDefs[ l].sidedef2;
  944.             if (sd >= 0)
  945.                UnSelectObject( &NewSel, SideDefs[ sd].sector);
  946.              }
  947.           ForgetSelection( &Selected);
  948.           Selected = NewSel;
  949.            }
  950.            /* select all LineDefs that have both ends selected */
  951.            else if (PrevMode == OBJ_VERTEXES && EditMode == OBJ_LINEDEFS)
  952.            {
  953.           int l;
  954.  
  955.           ObjectsNeeded( OBJ_LINEDEFS, 0);
  956.           NewSel = NULL;
  957.           for (l = 0; l < NumLineDefs; l++)
  958.              if (IsSelected( Selected, LineDefs[ l].start) && IsSelected( Selected, LineDefs[ l].end))
  959.             SelectObject( &NewSel, l);
  960.           ForgetSelection( &Selected);
  961.           Selected = NewSel;
  962.            }
  963.            /* unselect all */
  964.            else
  965.           ForgetSelection( &Selected);
  966.         }
  967.         if (GetMaxObjectNum( EditMode) >= 0)
  968.            CurObject = 0;
  969.         else
  970.            CurObject = -1;
  971.         OldObject = -1;
  972.         DragObject = FALSE;
  973.         StretchSelBox = FALSE;
  974.         RedrawMap = TRUE;
  975.      }
  976.  
  977.      /* user wants to display or hide the grid */
  978.      else if (keychar == 'G')
  979.      {
  980.         if ((altkey & 0x03) == 0x00)  /* no shift keys */
  981.         {
  982.            if (GridScale == 0)
  983.           GridScale = 256;
  984.            else if (GridScale > 8)
  985.           GridScale /= 2;
  986.            else
  987.           GridScale = 0;
  988.         }
  989.         else
  990.         {
  991.            if (GridScale == 0)
  992.           GridScale = 8;
  993.            else if (GridScale < 256)
  994.           GridScale *= 2;
  995.            else
  996.           GridScale = 0;
  997.         }
  998.         RedrawMap = TRUE;
  999.      }
  1000.      else if (keychar == 'H')
  1001.      {
  1002.         GridScale = 0;
  1003.         RedrawMap = TRUE;
  1004.      }
  1005.  
  1006.      /* user wants to toggle the rulers */
  1007.      else if (keychar == 'R')
  1008.         ShowRulers = !ShowRulers;
  1009.  
  1010.      /* user wants to toggle drag mode */
  1011.      else if (keychar == 'D')
  1012.      {
  1013.         StretchSelBox = FALSE;
  1014.         if (DragObject)
  1015.         {
  1016.            DragObject = FALSE;
  1017.            if (EditMode == OBJ_VERTEXES)
  1018.            {
  1019.           if (Selected == NULL && CurObject >= 0)
  1020.           {
  1021.              SelectObject( &Selected, CurObject);
  1022.              if (CheckMergedVertices( &Selected))
  1023.             RedrawMap = TRUE;
  1024.              ForgetSelection( &Selected);
  1025.           }
  1026.           else
  1027.              if (CheckMergedVertices( &Selected))
  1028.             RedrawMap = TRUE;
  1029.            }
  1030.         }
  1031.         else
  1032.         {
  1033.            DragObject = TRUE;
  1034.            if (EditMode == OBJ_THINGS && CurObject >= 0)
  1035.           MoveObjectsToCoords( EditMode, NULL, Things[ CurObject].xpos, Things[ CurObject].ypos, 0);
  1036.            else if (EditMode == OBJ_VERTEXES && CurObject >= 0)
  1037.           MoveObjectsToCoords( EditMode, NULL, Vertexes[ CurObject].x, Vertexes[ CurObject].y, 0);
  1038.            else
  1039.           MoveObjectsToCoords( EditMode, NULL, MAPX( PointerX), MAPY( PointerY), GridScale);
  1040.         }
  1041.      }
  1042.  
  1043.      /* user wants to select the next or previous object */
  1044.      else if (keychar == 'N' || keychar == '>')
  1045.      {
  1046.         if (CurObject < GetMaxObjectNum( EditMode))
  1047.         {
  1048.            CurObject++;
  1049.            GoToObject( EditMode, CurObject);
  1050.         }
  1051.         else if (GetMaxObjectNum( EditMode) >= 0)
  1052.            CurObject = 0;
  1053.         else
  1054.            CurObject = -1;
  1055.         RedrawMap = TRUE;
  1056.      }
  1057.      else if (keychar == 'P' || keychar == '<')
  1058.      {
  1059.         if (CurObject > 0)
  1060.         {
  1061.            CurObject--;
  1062.            GoToObject( EditMode, CurObject);
  1063.         }
  1064.         else
  1065.            CurObject = GetMaxObjectNum( EditMode);
  1066.         RedrawMap = TRUE;
  1067.      }
  1068.      else if (keychar == 'J' || keychar == '#')
  1069.      {
  1070.         OldObject = InputObjectNumber( -1, -1, EditMode, CurObject);
  1071.         if (OldObject >= 0)
  1072.         {
  1073.            CurObject = OldObject;
  1074.            GoToObject( EditMode, CurObject);
  1075.         }
  1076.         else
  1077.            OldObject = CurObject;
  1078.         RedrawMap = TRUE;
  1079.      }
  1080.  
  1081.      /* user wants to mark/unmark an object or a group of objects */
  1082.      else if (keychar == 'M')
  1083.      {
  1084.         if (StretchSelBox)
  1085.         {
  1086.            SelPtr oldsel;
  1087.  
  1088.            /* select all objects in the selection box */
  1089.            StretchSelBox = FALSE;
  1090.            RedrawMap = TRUE;
  1091.            /* additive selection box or not? */
  1092.            if (AdditiveSelBox == FALSE)
  1093.           ForgetSelection( &Selected);
  1094.            else
  1095.           oldsel = Selected;
  1096.            Selected = SelectObjectsInBox( EditMode, SelBoxX, SelBoxY, MAPX( PointerX), MAPY( PointerY));
  1097.            if (AdditiveSelBox == TRUE)
  1098.           while (oldsel != NULL)
  1099.           {
  1100.              if (! IsSelected( Selected, oldsel->objnum))
  1101.             SelectObject( &Selected, oldsel->objnum);
  1102.              UnSelectObject( &oldsel, oldsel->objnum);
  1103.           }
  1104.            if (Selected)
  1105.            {
  1106.           CurObject = Selected->objnum;
  1107.           PlaySound( 440, 10);
  1108.            }
  1109.            else
  1110.           CurObject = -1;
  1111.         }
  1112.         else if ((altkey & 0x03) == 0x00)  /* no shift keys */
  1113.         {
  1114.            if (CurObject >= 0)
  1115.            {
  1116.           /* mark or unmark one object */
  1117.           if (IsSelected( Selected, CurObject))
  1118.              UnSelectObject( &Selected, CurObject);
  1119.           else
  1120.              SelectObject( &Selected, CurObject);
  1121.           if (UseMouse)
  1122.              HideMousePointer();
  1123.           HighlightObject( EditMode, CurObject, GREEN);
  1124.           if (UseMouse)
  1125.              ShowMousePointer();
  1126.           if (Selected)
  1127.              PlaySound( 440, 10);
  1128.           DragObject = FALSE;
  1129.            }
  1130.            else
  1131.           Beep();
  1132.         }
  1133.         else
  1134.         {
  1135.            /* begin "stretch selection box" mode */
  1136.            SelBoxX = MAPX( PointerX);
  1137.            SelBoxY = MAPY( PointerY);
  1138.            StretchSelBox = TRUE;
  1139.            DragObject = FALSE;
  1140.         }
  1141.      }
  1142.  
  1143.      /* user wants to clear all marks and redraw the map */
  1144.      else if (keychar == 'C')
  1145.      {
  1146.         ForgetSelection( &Selected);
  1147.         RedrawMap = TRUE;
  1148.         DragObject = FALSE;
  1149.         StretchSelBox = FALSE;
  1150.      }
  1151.  
  1152.      /* user wants to copy a group of objects */
  1153.      else if (keychar == 'O' && CurObject >= 0)
  1154.      {
  1155.         /* copy the object(s) */
  1156.         if (Selected == NULL)
  1157.            SelectObject( &Selected, CurObject);
  1158.         CopyObjects( EditMode, Selected);
  1159.         /* enter drag mode */
  1160.         DragObject = TRUE;
  1161.         CurObject = Selected->objnum;
  1162.         if (EditMode == OBJ_THINGS)
  1163.            MoveObjectsToCoords( EditMode, NULL, Things[ CurObject].xpos, Things[ CurObject].ypos, 0);
  1164.         else if (EditMode == OBJ_VERTEXES)
  1165.            MoveObjectsToCoords( EditMode, NULL, Vertexes[ CurObject].x, Vertexes[ CurObject].y, 0);
  1166.         else
  1167.            MoveObjectsToCoords( EditMode, NULL, MAPX( PointerX), MAPY( PointerY), GridScale);
  1168.         RedrawMap = TRUE;
  1169.         StretchSelBox = FALSE;
  1170.      }
  1171.  
  1172.      /* user wants to edit the current object */
  1173.      else if ((key & 0x00FF) == 0x000D && CurObject >= 0) /* 'Enter' */
  1174.      {
  1175.         if (Selected)
  1176.            EditObjectsInfo( 0, 30, EditMode, Selected);
  1177.         else
  1178.         {
  1179.            SelectObject( &Selected, CurObject);
  1180.            EditObjectsInfo( 0, 30, EditMode, Selected);
  1181.            UnSelectObject( &Selected, CurObject);
  1182.         }
  1183.         RedrawMap = TRUE;
  1184.         DragObject = FALSE;
  1185.         StretchSelBox = FALSE;
  1186.      }
  1187.  
  1188.      /* user wants to delete the current object */
  1189.      else if ((key & 0xFF00) == 0x5300 && CurObject >= 0) /* 'Del' */
  1190.      {
  1191.         if (EditMode == OBJ_THINGS || Expert || Confirm( -1, -1,
  1192.                 (Selected ? "Do you really want to delete these objects?" : "Do you really want to delete this object?"),
  1193.                 (Selected ? "This will also delete the objects bound to them." : "This will also delete the objects bound to it.")))
  1194.         {
  1195.            if (Selected)
  1196.           DeleteObjects( EditMode, &Selected);
  1197.            else
  1198.           DeleteObject( EditMode, CurObject);
  1199.            CurObject = -1;
  1200.         }
  1201.         DragObject = FALSE;
  1202.         StretchSelBox = FALSE;
  1203.         RedrawMap = TRUE;
  1204.      }
  1205.  
  1206.      /* user wants to insert a new object */
  1207.      else if ((key & 0xFF00) == 0x5200) /* 'Ins' */
  1208.      {
  1209.         SelPtr cur;
  1210.  
  1211.         /* first special case: if several Vertices are selected, add new LineDefs */
  1212.         if (EditMode == OBJ_VERTEXES && Selected != NULL && Selected->next != NULL)
  1213.         {
  1214.            int firstv;
  1215.  
  1216.            ObjectsNeeded( OBJ_LINEDEFS, 0);
  1217.            if (Selected->next->next != NULL)
  1218.           firstv = Selected->objnum;
  1219.            else
  1220.           firstv = -1;
  1221.            EditMode = OBJ_LINEDEFS;
  1222.            /* create LineDefs between the Vertices */
  1223.            for (cur = Selected; cur->next; cur = cur->next)
  1224.            {
  1225.           /* check if there is already a LineDef between the two Vertices */
  1226.           for (CurObject = 0; CurObject < NumLineDefs; CurObject++)
  1227.              if ((LineDefs[ CurObject].start == cur->next->objnum && LineDefs[ CurObject].end == cur->objnum)
  1228.               || (LineDefs[ CurObject].end == cur->next->objnum && LineDefs[ CurObject].start == cur->objnum))
  1229.             break;
  1230.           if (CurObject < NumLineDefs)
  1231.              cur->objnum = CurObject;
  1232.           else
  1233.           {
  1234.              InsertObject( OBJ_LINEDEFS, -1, 0, 0);
  1235.              CurObject = NumLineDefs - 1;
  1236.              LineDefs[ CurObject].start = cur->next->objnum;
  1237.              LineDefs[ CurObject].end = cur->objnum;
  1238.              cur->objnum = CurObject;
  1239.           }
  1240.            }
  1241.            /* close the polygon if there are more than 2 Vertices */
  1242.            if (firstv >= 0 && (altkey & 0x03) != 0x00)  /* shift key pressed */
  1243.            {
  1244.           for (CurObject = 0; CurObject < NumLineDefs; CurObject++)
  1245.              if ((LineDefs[ CurObject].start == firstv && LineDefs[ CurObject].end == cur->objnum)
  1246.               || (LineDefs[ CurObject].end == firstv && LineDefs[ CurObject].start == cur->objnum))
  1247.             break;
  1248.           if (CurObject < NumLineDefs)
  1249.              cur->objnum = CurObject;
  1250.           else
  1251.           {
  1252.              InsertObject( OBJ_LINEDEFS, -1, 0, 0);
  1253.              CurObject = NumLineDefs - 1;
  1254.              LineDefs[ CurObject].start = firstv;
  1255.              LineDefs[ CurObject].end = cur->objnum;
  1256.              cur->objnum = CurObject;
  1257.           }
  1258.            }
  1259.            else
  1260.           UnSelectObject( &Selected, cur->objnum);
  1261.         }
  1262.         /* second special case: if several LineDefs are selected, add new SideDefs and one Sector */
  1263.         else if (EditMode == OBJ_LINEDEFS && Selected != NULL)
  1264.         {
  1265.            ObjectsNeeded( OBJ_LINEDEFS, 0);
  1266.            for (cur = Selected; cur; cur = cur->next)
  1267.           if (LineDefs[ cur->objnum].sidedef1 >= 0 && LineDefs[ cur->objnum].sidedef2 >= 0)
  1268.           {
  1269.              char msg[ 80];
  1270.  
  1271.              Beep();
  1272.              sprintf( msg, "LineDef #%d already has two SideDefs", cur->objnum);
  1273.              Notify( -1, -1, "Error: cannot add the new Sector", msg);
  1274.              break;
  1275.           }
  1276.            if (cur == NULL)
  1277.            {
  1278.           EditMode = OBJ_SECTORS;
  1279.           InsertObject( OBJ_SECTORS, -1, 0, 0);
  1280.           CurObject = NumSectors - 1;
  1281.           for (cur = Selected; cur; cur = cur->next)
  1282.           {
  1283.              InsertObject( OBJ_SIDEDEFS, -1, 0, 0);
  1284.              SideDefs[ NumSideDefs - 1].sector = CurObject;
  1285.              ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1286.              if (LineDefs[ cur->objnum].sidedef1 >= 0)
  1287.              {
  1288.             int s;
  1289.  
  1290.             s = SideDefs[ LineDefs[ cur->objnum].sidedef1].sector;
  1291.             if (s >= 0)
  1292.             {
  1293.                Sectors[ CurObject].floorh = Sectors[ s].floorh;
  1294.                Sectors[ CurObject].ceilh = Sectors[ s].ceilh;
  1295.                strncpy( Sectors[ CurObject].floort, Sectors[ s].floort, 8);
  1296.                strncpy( Sectors[ CurObject].ceilt, Sectors[ s].ceilt, 8);
  1297.                Sectors[ CurObject].light = Sectors[ s].light;
  1298.             }
  1299.             LineDefs[ cur->objnum].sidedef2 = NumSideDefs - 1;
  1300.             LineDefs[ cur->objnum].flags = 4;
  1301.             strncpy( SideDefs[ NumSideDefs - 1].tex3, "-", 8);
  1302.             strncpy( SideDefs[ LineDefs[ cur->objnum].sidedef1].tex3, "-", 8);
  1303.              }
  1304.              else
  1305.             LineDefs[ cur->objnum].sidedef1 = NumSideDefs - 1;
  1306.           }
  1307.           ForgetSelection( &Selected);
  1308.           SelectObject( &Selected, CurObject);
  1309.            }
  1310.         }
  1311.         /* normal case: add a new object of the current type */
  1312.         else
  1313.         {
  1314.            ForgetSelection( &Selected);
  1315.            if (GridScale > 0)
  1316.           InsertObject( EditMode, CurObject, (MAPX( PointerX) + GridScale / 2) & ~(GridScale - 1), (MAPY( PointerY) + GridScale / 2) & ~(GridScale - 1));
  1317.            else
  1318.           InsertObject( EditMode, CurObject, MAPX( PointerX), MAPY( PointerY));
  1319.            CurObject = GetMaxObjectNum( EditMode);
  1320.            if (EditMode == OBJ_LINEDEFS)
  1321.            {
  1322.           if (! Input2VertexNumbers( -1, -1, "Choose the two vertices for the new LineDef",
  1323.                          &(LineDefs[ CurObject].start), &(LineDefs[ CurObject].end)))
  1324.           {
  1325.              DeleteObject( EditMode, CurObject);
  1326.              CurObject = -1;
  1327.           }
  1328.            }
  1329.         }
  1330.         DragObject = FALSE;
  1331.         StretchSelBox = FALSE;
  1332.         RedrawMap = TRUE;
  1333.      }
  1334.  
  1335.      /* user likes music */
  1336.      else if (key)
  1337.         Beep();
  1338.  
  1339.      /* redraw the (keyboard) pointer */
  1340.      if (FakeCursor || ShowRulers)
  1341.      {
  1342.         HideMousePointer();
  1343.         DrawPointer( ShowRulers);
  1344.         ShowMousePointer();
  1345.      }
  1346.       }
  1347.  
  1348.       /* check if Scroll Lock is off */
  1349.       if ((bioskey( 2) & 0x10) == 0x00)
  1350.       {
  1351.      /* move the map if the pointer is near the edge of the screen */
  1352.      if (PointerY <= (UseMouse ? 2 : 20))
  1353.      {
  1354.         if (! UseMouse)
  1355.            PointerY += MoveSpeed;
  1356.         if (MAPY( ScrCenterY) < MapMaxY)
  1357.         {
  1358.            OrigY += MoveSpeed * 2 * Scale;
  1359.            RedrawMap = TRUE;
  1360.         }
  1361.      }
  1362.      if (PointerY >= ScrMaxY - (UseMouse ? 8 : 20))
  1363.      {
  1364.         if (! UseMouse)
  1365.            PointerY -= MoveSpeed;
  1366.         if (MAPY( ScrCenterY) > MapMinY)
  1367.         {
  1368.            OrigY -= MoveSpeed * 2 * Scale;
  1369.            RedrawMap = TRUE;
  1370.         }
  1371.      }
  1372.      if (PointerX <= (UseMouse ? 8 : 20))
  1373.      {
  1374.         if (! UseMouse)
  1375.            PointerX += MoveSpeed;
  1376.         if (MAPX( ScrCenterX) > MapMinX)
  1377.         {
  1378.            OrigX -= MoveSpeed * 2 * Scale;
  1379.            RedrawMap = TRUE;
  1380.         }
  1381.      }
  1382.      if (PointerX >= ScrMaxX - (UseMouse ? 8 : 20))
  1383.      {
  1384.         if (! UseMouse)
  1385.            PointerX -= MoveSpeed;
  1386.         if (MAPX( ScrCenterX) < MapMaxX)
  1387.         {
  1388.            OrigX += MoveSpeed * 2 * Scale;
  1389.            RedrawMap = TRUE;
  1390.         }
  1391.      }
  1392.       }
  1393.    }
  1394. }
  1395.  
  1396.  
  1397.  
  1398. /*
  1399.    draw the actual game map
  1400. */
  1401.  
  1402. void DrawMap( int editmode, int grid) /* SWAP! */
  1403. {
  1404.    int  n, m;
  1405.    char texname[9];
  1406.  
  1407.    /* clear the screen */
  1408.    ClearScreen();
  1409.  
  1410.    /* draw the grid */
  1411.    if (grid > 0)
  1412.    {
  1413.       int mapx0 = MAPX( 0) & ~(grid - 1);
  1414.       int mapx1 = (MAPX( ScrMaxX) + grid) & ~(grid - 1);
  1415.       int mapy0 = (MAPY( ScrMaxY) - grid) & ~(grid - 1);
  1416.       int mapy1 = MAPY( 0) & ~(grid - 1);
  1417.  
  1418.       SetColor( BLUE);
  1419.       for (n = mapx0; n <= mapx1; n += grid)
  1420.      DrawMapLine( n, mapy0, n, mapy1);
  1421.       for (n = mapy0; n <= mapy1; n += grid)
  1422.      DrawMapLine( mapx0, n, mapx1, n);
  1423.    }
  1424.  
  1425.    /* draw the linedefs to form the map */
  1426.    switch (editmode)
  1427.    {
  1428.    case OBJ_THINGS:
  1429.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  1430.       for (n = 0; n < NumLineDefs; n++)
  1431.       {
  1432.      if (LineDefs[ n].flags & 1)
  1433.         SetColor( WHITE);
  1434.      else
  1435.         SetColor( LIGHTGRAY);
  1436.      DrawMapLine( Vertexes[ LineDefs[ n].start].x, Vertexes[ LineDefs[ n].start].y,
  1437.               Vertexes[ LineDefs[ n].end].x, Vertexes[ LineDefs[ n].end].y);
  1438.       }
  1439.       break;
  1440.    case OBJ_VERTEXES:
  1441.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  1442.       SetColor( LIGHTGRAY);
  1443.       for (n = 0; n < NumLineDefs; n++)
  1444.      DrawMapVector( Vertexes[ LineDefs[ n].start].x, Vertexes[ LineDefs[ n].start].y,
  1445.             Vertexes[ LineDefs[ n].end].x, Vertexes[ LineDefs[ n].end].y);
  1446.       break;
  1447.    case OBJ_LINEDEFS:
  1448.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  1449.       for (n = 0; n < NumLineDefs; n++)
  1450.       {
  1451.      if (LineDefs[ n].type > 0)
  1452.      {
  1453.         if (LineDefs[ n].tag > 0)
  1454.            SetColor( LIGHTMAGENTA);
  1455.         else
  1456.            SetColor( LIGHTGREEN);
  1457.      }
  1458.      else if (LineDefs[ n].tag > 0)
  1459.         SetColor( LIGHTRED);
  1460.      else if (LineDefs[ n].flags & 1)
  1461.         SetColor( WHITE);
  1462.      else
  1463.         SetColor( LIGHTGRAY);
  1464.      DrawMapLine( Vertexes[ LineDefs[ n].start].x, Vertexes[ LineDefs[ n].start].y,
  1465.               Vertexes[ LineDefs[ n].end].x, Vertexes[ LineDefs[ n].end].y);
  1466.       }
  1467.       break;
  1468.    case OBJ_SECTORS:
  1469.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1470.       for (n = 0; n < NumLineDefs; n++)
  1471.       {
  1472.      if ((m = LineDefs[ n].sidedef1) < 0 || (m = SideDefs[ m].sector) < 0)
  1473.         SetColor( LIGHTRED);
  1474.      else
  1475.      {
  1476.         if (Sectors[ m].tag > 0)
  1477.            SetColor( LIGHTGREEN);
  1478.         else if (Sectors[ m].special > 0)
  1479.            SetColor( LIGHTCYAN);
  1480.         else if (LineDefs[ n].flags & 1)
  1481.            SetColor( WHITE);
  1482.         else
  1483.            SetColor( LIGHTGRAY);
  1484.         if ((m = LineDefs[ n].sidedef2) >= 0)
  1485.         {
  1486.            if ((m = SideDefs[ m].sector) < 0)
  1487.           SetColor( LIGHTRED);
  1488.            else if (Sectors[ m].tag > 0)
  1489.           SetColor( LIGHTGREEN);
  1490.            else if (Sectors[ m].special > 0)
  1491.           SetColor( LIGHTCYAN);
  1492.         }
  1493.      }
  1494.      ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  1495.      DrawMapLine( Vertexes[ LineDefs[ n].start].x, Vertexes[ LineDefs[ n].start].y,
  1496.               Vertexes[ LineDefs[ n].end].x, Vertexes[ LineDefs[ n].end].y);
  1497.       }
  1498.       break;
  1499.    }
  1500.  
  1501.    /* draw in the vertices */
  1502.    if (editmode == OBJ_VERTEXES)
  1503.    {
  1504.       SetColor( LIGHTGREEN);
  1505.       for (n = 0; n < NumVertexes; n++)
  1506.       {
  1507.      DrawMapLine( Vertexes[ n].x - OBJSIZE, Vertexes[ n].y - OBJSIZE, Vertexes[ n].x + OBJSIZE, Vertexes[ n].y + OBJSIZE);
  1508.      DrawMapLine( Vertexes[ n].x + OBJSIZE, Vertexes[ n].y - OBJSIZE, Vertexes[ n].x - OBJSIZE, Vertexes[ n].y + OBJSIZE);
  1509.       }
  1510.    }
  1511.  
  1512.    /* draw in the things */
  1513.    ObjectsNeeded( OBJ_THINGS, 0);
  1514.    if (editmode == OBJ_THINGS)
  1515.    {
  1516.       for (n = 0; n < NumThings; n++)
  1517.       {
  1518.      m = GetThingRadius( Things[ n].type);
  1519.      SetColor( GetThingColour( Things[ n].type));
  1520.      DrawMapLine( Things[ n].xpos - m, Things[ n].ypos, Things[ n].xpos + m, Things[ n].ypos);
  1521.      DrawMapLine( Things[ n].xpos, Things[ n].ypos - m, Things[ n].xpos, Things[ n].ypos + m);
  1522.      DrawMapCircle( Things[ n].xpos, Things[ n].ypos, m);
  1523.       }
  1524.    }
  1525.    else
  1526.    {
  1527.       SetColor( LIGHTGRAY);
  1528.       for (n = 0; n < NumThings; n++)
  1529.       {
  1530.      DrawMapLine( Things[ n].xpos - OBJSIZE, Things[ n].ypos, Things[ n].xpos + OBJSIZE, Things[ n].ypos);
  1531.      DrawMapLine( Things[ n].xpos, Things[ n].ypos - OBJSIZE, Things[ n].xpos, Things[ n].ypos + OBJSIZE);
  1532.       }
  1533.    }
  1534.  
  1535.    /* draw in the title bar */
  1536.    DrawScreenBox3D( 0, 0, ScrMaxX, 16);
  1537.    SetColor( WHITE);
  1538.    DrawScreenText( 20,  4, "File  Edit  Search  Modes  Misc  Objects  Check");
  1539.    DrawScreenText( 20,  6, "_     _     _       _       _    _        _    ");
  1540.    DrawScreenText( ScrMaxX - 45, 4, "Help");
  1541.    DrawScreenText( ScrMaxX - 45, 6, "_   ");
  1542.  
  1543.    /* draw the bottom line, if needed */
  1544.    if (InfoShown)
  1545.    {
  1546.       DrawScreenBox3D( 0, ScrMaxY - 11, ScrMaxX, ScrMaxY);
  1547.       DrawScreenText( 5, ScrMaxY - 8, "Editing %s on %s", GetEditModeName( editmode), Level->dir.name);
  1548.       DrawScreenText( ScrMaxX - 176, ScrMaxY - 8, "Scale: 1/%d  Grid: %d", Scale, grid);
  1549.       if (farcoreleft() < 50000)
  1550.       {
  1551.      if (farcoreleft() < 20000)
  1552.         SetColor( LIGHTRED);
  1553.      else
  1554.         SetColor( RED);
  1555.       }
  1556.       DrawScreenText( ScrCenterX - ((editmode == OBJ_LINEDEFS) ? 10 : 50), ScrMaxY - 8, "Free mem: %lu", farcoreleft());
  1557.    }
  1558. }
  1559.  
  1560.  
  1561.  
  1562. /*
  1563.    center the map around the given coords
  1564. */
  1565.  
  1566. void CenterMapAroundCoords( int xpos, int ypos)
  1567. {
  1568.    OrigX = xpos;
  1569.    OrigY = ypos;
  1570.    PointerX = ScrCenterX;
  1571.    PointerY = ScrCenterY;
  1572. }
  1573.  
  1574.  
  1575.  
  1576. /*
  1577.    center the map around the object and zoom in if necessary
  1578. */
  1579.  
  1580. void GoToObject( int objtype, int objnum) /* SWAP! */
  1581. {
  1582.    int xpos, ypos;
  1583.    int xpos2, ypos2;
  1584.    int n, oldscale;
  1585.    int sd1, sd2;
  1586.  
  1587.    GetObjectCoords( objtype, objnum, &xpos, &ypos);
  1588.    CenterMapAroundCoords( xpos, ypos);
  1589.    oldscale = Scale;
  1590.    /* zoom in until the object can be selected */
  1591.    while (Scale > 1 && GetCurObject( objtype, MAPX( PointerX - 4), MAPY( PointerY - 4), MAPX( PointerX + 4), MAPY( PointerY + 4)) != objnum)
  1592.       Scale--;
  1593.    /* Special case for Sectors: if several Sectors are one inside another, */
  1594.    /* then zooming in won't help.  So I choose a LineDef that borders the  */
  1595.    /* Sector and move a few pixels towards the inside of the Sector.       */
  1596.    if (objtype == OBJ_SECTORS && GetCurObject( OBJ_SECTORS, OrigX, OrigY, OrigX, OrigY) != objnum)
  1597.    {
  1598.       /* restore the Scale */
  1599.       Scale = oldscale;
  1600.       for (n = 0; n < NumLineDefs; n++)
  1601.       {
  1602.      ObjectsNeeded( OBJ_LINEDEFS, 0);
  1603.      sd1 = LineDefs[ n].sidedef1;
  1604.      sd2 = LineDefs[ n].sidedef2;
  1605.      ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1606.      if (sd1 >= 0 && SideDefs[ sd1].sector == objnum)
  1607.         break;
  1608.      if (sd2 >= 0 && SideDefs[ sd2].sector == objnum)
  1609.         break;
  1610.       }
  1611.       if (n < NumLineDefs)
  1612.       {
  1613.      GetObjectCoords( OBJ_LINEDEFS, n, &xpos2, &ypos2);
  1614.      n = ComputeDist( abs( xpos - xpos2), abs( ypos - ypos2)) / 7;
  1615.      if (n <= 1)
  1616.        n = 2;
  1617.      xpos = xpos2 + (xpos - xpos2) / n;
  1618.      ypos = ypos2 + (ypos - ypos2) / n;
  1619.      CenterMapAroundCoords( xpos, ypos);
  1620.      /* zoom in until the sector can be selected */
  1621.      while (Scale > 1 && GetCurObject( OBJ_SECTORS, OrigX, OrigY, OrigX, OrigY) != objnum)
  1622.         Scale--;
  1623.       }
  1624.    }
  1625.    if (UseMouse)
  1626.       SetMouseCoords( PointerX, PointerY);
  1627. }
  1628.  
  1629.  
  1630.  
  1631. /* end of file */
  1632.